package controller;

import java.io.IOException;
import java.io.PrintWriter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserDTO;
import bizlogic.AdminManager;


public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		//String aa = request.getParameter("rdoGender");
		//PrintWriter out = response.getWriter();
        //out.println("<h1>" + aa + "</h1>");
		AdminManager adm = new AdminManager();
		Date date = null;
		String str =request.getParameter("DOB");
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); //please notice the capital M
		
		try {
			date = formatter.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			System.out.println("ERROR............................");
			e.printStackTrace();
		}
		
		 
		PrintWriter out = response.getWriter();
        out.println("<h1> ---" + date.toString() + "---</h1>");		
		
        System.out.println("Name" + request.getParameter("uName"));
		System.out.println("Password" + request.getParameter("uPassword"));
		System.out.println("txtEmail" + request.getParameter("txtEmail"));
		System.out.println("rodGender" + request.getParameter("rdoGender"));
		System.out.println("date" + request.getParameter("date"));
		
		UserDTO user = new UserDTO();
		user.setuName(request.getParameter("uName"));
		user.setuPassword(request.getParameter("uPassword"));
		user.setEmail(request.getParameter("txtEmail"));
		user.setGender(Boolean.parseBoolean(request.getParameter("rdoGender")));
		user.setDOB(new Date());
		
		System.out.println("Name" + request.getParameter("uName"));
		System.out.println("Password" + request.getParameter("uPassword"));
		System.out.println("txtEmail" + request.getParameter("txtEmail"));
		System.out.println("rodGender" + request.getParameter("rdoGender"));
		System.out.println("date" + request.getParameter("date"));
		
		
		adm.insertUser(user);
		
	}

}
